using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Plain;
using Plain.Data;
using Plain.Search;
using Plain.Web.Mvc.Controllers;

using Plain.Demo.Blog.Business;
using Plain.Demo.Blog.Web;

namespace Plain.Demo.Blog.Web.UI
{
	[HandleError]
	public class PostController : PlainController<Post>
	{
		public ActionResult Index ()
		{
			ISearchableDao<Post, int> dao = PlainApplication.Config.DaoFactory.GetDao<Post, int> ();
			var posts = dao.GetAll();
			
			return View (posts);
		}

		public ActionResult Create ()
		{
			return View ();
		}
		
		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create (string submitButton, Post post,string Tags)
		{
			try {
				ISearchableDao<User, int> dao = PlainApplication.Config.DaoFactory.GetDao<User, int> ();
				var user = dao.Get (1);
				post.Author = user;
				post.Created = DateTime.Now;
				post.Tags = Tags.Replace(" ","").Split(',').ToList();
				
				switch (submitButton) {
					case "Publish":
						if(!post.Published.HasValue){
							post.Published = DateTime.Now;
						}
						break;
					case "Schedule":
						if(!post.Published.HasValue){
						
						}
						break;
					default:
						if(!post.Published.HasValue){
							post.Published = null;
						}
						break;
				}
				
				post.Save ();
				
				return RedirectToAction ("Index");
			} catch {
				return View ();
			}
		}

		public ActionResult Edit (int id)
		{
			ISearchableDao<Post, int> dao = PlainApplication.Config.DaoFactory.GetDao<Post, int> ();
			var post = dao.Get (id);
			return View (post);
		}
		
		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit (int id, string submitButton, Post post, string Tags)
		{
			try {
				ISearchableDao<User, int> dao = PlainApplication.Config.DaoFactory.GetDao<User, int> ();
				var user = dao.Get (1);
				post.Author = user;
				post.Created = DateTime.Now;
				post.Tags = Tags.Replace(" ","").Split(',').ToList();
						              
				switch (submitButton) {
					case "Publish":
						if(!post.Published.HasValue){
							post.Published = DateTime.Now;
						}
						break;		
					case "Schedule":
						if(!post.Published.HasValue){
						
						}
						break;
					default:
						if(!post.Published.HasValue){
							post.Published = null;
						}
						break;
				}
				
				post.Save ();
				
				return RedirectToAction ("Index");
			} catch {
				return View ();
			}
		}
	
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(Post post){
			post.Delete();
			return RedirectToAction ("Index");
		}
		
		public PlainJsonResult Scheduled(){
			ISearchableDao<Post, int> dao = PlainApplication.Config.DaoFactory.GetDao<Post, int> ();
			var posts = dao.GetAll ().Where(x=> x.Published.HasValue && x.Published.Value > DateTime.Now);
		
			return new PlainJsonResult(){ Data=posts };		
		}
		
		public PlainJsonResult Draft(){
			ISearchableDao<Post, int> dao = PlainApplication.Config.DaoFactory.GetDao<Post, int> ();
			var posts = dao.GetAll ().Where(x=> !x.Published.HasValue);
			
			return new PlainJsonResult(){ Data=posts };	
		}
		
		
		public PlainJsonResult Published(){
			ISearchableDao<Post, int> dao = PlainApplication.Config.DaoFactory.GetDao<Post, int> ();
			var posts = dao.GetAll ().Where(x=> x.Published.HasValue && x.Published.Value <= DateTime.Now);;
			
			return new PlainJsonResult(){ Data=posts };	
		}
	}
}